#include "SessionLayer.h"
#include "ProtocolFactory.h"


CSessionLayer::CSessionLayer(void)
{
	name = "SESSION";
}

CSessionLayer::~CSessionLayer(void)
{
	layers.clear();
}

void CSessionLayer::addProtocol( const boost::shared_ptr<IProtocol>& protocol )
{
	add(protocol);
}

const boost::shared_ptr<IProtocol>& CSessionLayer::getProtocol( const std::string& name ) const
{
	citerator it = std::find_if(cbegin(), cend(), ProtocolComparer(name));

	if(it == cend()) 
	{
		throw new std::exception();
	}

	return *it;
}

void CSessionLayer::removeProtocol( const std::string& name )
{
	getContainerToModify().erase(std::remove_if(begin(), end(), ProtocolComparer(name)));
}

StackUtils::e_Status CSessionLayer::Decode( CPacket& packet )
{
	std::for_each(rbegin(), rend(), ProtocolDecode(packet));
	return StackUtils::E_SUCCESS;
}

StackUtils::e_Status CSessionLayer::Encode( CPacket& packet )
{
	std::for_each(begin(), end(), ProtocolEncode(packet));
	return StackUtils::E_SUCCESS;
}

void CSessionLayer::add( const boost::shared_ptr<IProtocol>& element )
{
	getContainerToModify().push_back(element);
}

void CSessionLayer::init()
{
	boost::shared_ptr<IProtocol> SSL_ptr(CProtocolFactory::CreateProtocol("SSL"));
	add(SSL_ptr);
}
